package com.youdianda.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youdianda.common.CustomException;
import com.youdianda.common.PageResult;
import com.youdianda.common.R;
import com.youdianda.dto.YddAdvimgDto;
import com.youdianda.dto.YddAdvposDto;
import com.youdianda.entity.YddAdvimg;
import com.youdianda.entity.YddAdvpos;
import com.youdianda.service.YddAdvimgService;
import com.youdianda.service.YddAdvposService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/ydd_advimg")
@Slf4j
public class YddAdvimgController {
    @Autowired
    private YddAdvimgService yddAdvimgService;

    @Autowired
    private YddAdvposService yddAdvposService;


    /**
     * 广告图列表分页
     *
     * @return
     */
    @GetMapping("/list")
    public R page(String advimgdesc, String advimgpos, int page, int limit) {
        //构造分页构造器对象
        Page<YddAdvimg> pageInfo = new Page<>(page, limit);
//        Page<YddAdvimgDto> advimgDtoPage = new Page<>();
        //条件构造器
        LambdaQueryWrapper<YddAdvimg> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(advimgdesc), YddAdvimg::getAdvimgdesc, advimgdesc);
        queryWrapper.like(StringUtils.isNotEmpty(advimgpos), YddAdvimg::getAdvimgpos, advimgpos);
        //添加排序条件
        queryWrapper.orderByDesc(YddAdvimg::getId);
        //执行分页查询
        yddAdvimgService.page(pageInfo, queryWrapper);

        List<YddAdvimgDto> yddAdvimgDtos = pageInfo.getRecords().stream().map(item -> {
            YddAdvimgDto yddAdvimgDto = new YddAdvimgDto();
            BeanUtils.copyProperties(item, yddAdvimgDto);
            //查询该广告位id对应的广告图位置
            LambdaQueryWrapper<YddAdvpos> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(YddAdvpos::getId, item.getAdvimgpos());
            YddAdvpos yddAdvpos = yddAdvposService.getOne(queryWrapper1);
            //打印广告名称对应日志
            log.info("yaaAdvpos = " + yddAdvpos);
            yddAdvimgDto.setAdvposname(yddAdvpos.getAdvposname());
            return yddAdvimgDto;
        }).collect(Collectors.toList());

//        Page<YddAdvimgDto> advimgDtoPage1 = advimgDtoPage.setRecords(yddAdvimgDtos);
//        return R.success(advimgDtoPage1);

        //将数据封装为pageResult
        PageResult pageResult = PageResult.getResultByDtoPage(pageInfo, yddAdvimgDtos);
        return R.success(pageResult);
    }

    /**
     * 添加广告图
     *
     * @param yddAdvimg
     * @return
     */
    @PostMapping("/add")
    public R<String> save(@RequestBody YddAdvimg yddAdvimg) {
        log.info(yddAdvimg.toString());

        yddAdvimgService.save(yddAdvimg);

        return R.success("广告图新增成功");
    }

    /**
     * 广告图详情
     * 修改数据回显
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public R<YddAdvimg> getById(@RequestParam Long id) {
        log.info("id{}:", id);
        YddAdvimg yddAdvimg = yddAdvimgService.getById(id);

        return R.success(yddAdvimg);
    }

    /**
     * 修改广告图数据
     *
     * @param yddAdvimg
     * @return
     */
    @PostMapping("/update")
    public R<String> update(@RequestBody YddAdvimg yddAdvimg) {
        yddAdvimgService.updateById(yddAdvimg);

        return R.success("修改广告图数据成功");
    }

    /**
     * 根据id删除数据
     *
     * @param id
     * @return
     */
    @GetMapping("/del")
    public R<String> delete(@RequestParam Long id) {
        //条件构造器
        LambdaQueryWrapper<YddAdvimg> queryWrapper = new LambdaQueryWrapper<>();
        //构造条件进行查询
        queryWrapper.in(YddAdvimg::getId, id);
        List<YddAdvimg> yddAdvimgList = yddAdvimgService.list(queryWrapper);
        //进行遍历
        for (YddAdvimg yddAdvimg : yddAdvimgList) {
            if ("1".equals(yddAdvimg.getDefaultData())) {
                throw new CustomException("系统默认数据无法删除");
            }
        }
        yddAdvimgService.removeById(id);
        return R.success("删除数据成功");
    }

}
